package com.design.adnroid.campussecondarytrading.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import com.design.adnroid.campussecondarytrading.entity.Goods;
import com.design.adnroid.campussecondarytrading.entity.Person;

import java.io.ByteArrayOutputStream;

public class DBOperate {

    private static final String DB_NAME = "Sql.db";
    private static final String USERS_TABLE_NAME = "users";
    private static final String GOODS_TABLE_NAME = "goods";
    private static final int DB_VERSION = 1;
    private SQLiteDatabase db;
    private DBOpenHelper dbOpenHelper;

    public DBOperate(Context context) {
        dbOpenHelper = new DBOpenHelper(context);
    }

    public static String getDbName() {
        return DB_NAME;
    }

    public SQLiteDatabase getDb() {
        return db;
    }

    public String getGoodsTableName() {
        return GOODS_TABLE_NAME;
    }

    public String getUsersTableName() {
        return USERS_TABLE_NAME;
    }

    public void openDB() {
        db = dbOpenHelper.getWritableDatabase();
    }

    public void closeDb() {
        db.close();
    }

    //  添加users一条用户信息
    public void addPerson(Person person) {
        ContentValues contentValues = new ContentValues();
        contentValues.put("userName", person.getUserName());
        contentValues.put("userMd5", person.getUserMd5());
        contentValues.put("userPwd", person.getUserPwd());
        contentValues.put("name", person.getName());
        contentValues.put("sex", person.getSex());
        db.insert(USERS_TABLE_NAME, null, contentValues);
    }

    // 删除users一条用户信息
    public boolean deletePerson(Person person) {
        String userMd5 = person.getUserMd5();
        boolean b = findPerson(person.getUserName(), person.getUserPwd());
        if (b) {
            db.delete(USERS_TABLE_NAME, "userMd5=?", new String[]{userMd5});
            return true;
        } else {
            return false;
        }
    }

    // 更新users一条用户信息
    public boolean upPerson(Person person) {
        ContentValues contentValues = new ContentValues();
        String userMd5 = person.getUserMd5();
        contentValues.put("userName", person.getUserName());
        contentValues.put("userMd5", userMd5);
        contentValues.put("userPwd", person.getUserPwd());
        contentValues.put("name", person.getName());
        contentValues.put("sex", person.getSex());
        db.update(USERS_TABLE_NAME, contentValues, "userMd5=?", new String[]{userMd5});
        return true;
    }

    // 得到指定名字的用户信息
    public Person getPerson(String userName) {

        if (havePerson(userName)) {
            Cursor cursor = db.query(USERS_TABLE_NAME,
                    null,
                    "userName=?",
                    new String[]{userName}, null, null, null);
            cursor.moveToFirst();
            Person person = new Person();
            person.setUserName(cursor.getString(cursor.getColumnIndex("userName")));
            person.setUserPwd(cursor.getString(cursor.getColumnIndex("userPwd")));
            person.setName(cursor.getString(cursor.getColumnIndex("name")));
            person.setSex(cursor.getString(cursor.getColumnIndex("sex")));
            cursor.close();
            return person;
        } else {
            return new Person();
        }
    }

    // 判断users表中的是否包含某个userName,userPwd的记录
    public Boolean findPerson(String userName, String userPwd) {
        Boolean b = false;
        Cursor cursor = db.query(USERS_TABLE_NAME,
                new String[]{"userName,userPwd"},
                "userName=? and userPwd=?",
                new String[]{userName, userPwd}, null, null, null);
        b = cursor.moveToFirst();
        cursor.close();
        return b;
    }

    // 判断users表中的是否包含某个userName的记录
    public Boolean havePerson(String userName) {
        Boolean b = false;
//        Cursor cursor = db.query(TABLE_NAME, null, "name" + "=" + name, null, null, null, null);//局限，name中不能有字母
        Cursor cursor = db.query(USERS_TABLE_NAME, new String[]{"userName"}, "userName=?", new String[]{userName}, null, null, null);
//        db.query(TABLE_NAME, new String[]{"字段1,字段2"}, "条件1=? and 条件2=? ",
//                              new String[]{"条件1的值 and 条件2的值 "}, null, null, null);
        b = cursor.moveToFirst();
        cursor.close();
        return b;
    }

    public void addGoods(Goods goods) {
        ContentValues contentValues = new ContentValues();

        ByteArrayOutputStream os = new ByteArrayOutputStream();
        goods.getGoodsImage().compress(Bitmap.CompressFormat.PNG, 100, os);
        byte[] by = os.toByteArray();

        contentValues.put("goodsId", goods.getGoodsId());
        contentValues.put("goodsName", goods.getGoodsName());
        contentValues.put("goodsText", goods.getGoodsText());
        contentValues.put("goodsImage", by);
        contentValues.put("goodsTime", goods.getGoodsTime());
        contentValues.put("goodsPrice", goods.getGoodsPrice());
        contentValues.put("goodsOwner", goods.getGoodsOwner());
        contentValues.put("goodsOwnerContact", goods.getGoodsOwnerContact());
        contentValues.put("goodsState", String.valueOf(goods.getGoodsState()));
        contentValues.put("goodsBuyers", goods.getGoodsBuyers());

        db.insert(GOODS_TABLE_NAME, null, contentValues);
    }

    // 得到指定goodsId的商品信息
    public Goods getGoods(String goodsId) {
        Cursor cursor = db.query(GOODS_TABLE_NAME,
                null, "goodsId=?",
                new String[]{goodsId}, null, null, null);

        cursor.moveToFirst();
        Goods goods = new Goods();
        byte[] bytes = cursor.getBlob(cursor.getColumnIndex("goodsImage"));
        Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, null);

        goods.setGoodsId(cursor.getString(cursor.getColumnIndex("goodsId")));
        goods.setGoodsName(cursor.getString(cursor.getColumnIndex("goodsName")));
        goods.setGoodsText(cursor.getString(cursor.getColumnIndex("goodsText")));
        goods.setGoodsImage(bitmap);
        goods.setGoodsTime(cursor.getString(cursor.getColumnIndex("goodsTime")));
        goods.setGoodsPrice(cursor.getDouble(cursor.getColumnIndex("goodsPrice")));
        goods.setGoodsOwner(cursor.getString(cursor.getColumnIndex("goodsOwner")));
        goods.setGoodsOwnerContact(cursor.getString(cursor.getColumnIndex("goodsOwnerContact")));
        goods.setGoodsState(cursor.getInt(cursor.getColumnIndex("goodsState")));
        goods.setGoodsBuyers(cursor.getString(cursor.getColumnIndex("goodsBuyers")));

        cursor.close();
        return goods;
    }

    // 更新商品一条用户信息
    public boolean upGoods(Goods goods) {

        ContentValues contentValues = new ContentValues();

        String goodsId = goods.getGoodsId();
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        goods.getGoodsImage().compress(Bitmap.CompressFormat.PNG, 100, os);
        byte[] by = os.toByteArray();

        contentValues.put("goodsId", goodsId);
        contentValues.put("goodsName", goods.getGoodsName());
        contentValues.put("goodsText", goods.getGoodsText());
        contentValues.put("goodsImage", by);
        contentValues.put("goodsTime", goods.getGoodsTime());
        contentValues.put("goodsPrice", goods.getGoodsPrice());
        contentValues.put("goodsOwner", goods.getGoodsOwner());
        contentValues.put("goodsOwnerContact", goods.getGoodsOwnerContact());
        contentValues.put("goodsState", String.valueOf(goods.getGoodsState()));
        contentValues.put("goodsBuyers", goods.getGoodsBuyers());

        db.update(GOODS_TABLE_NAME, contentValues, "goodsId=?", new String[]{goodsId});
        return true;
    }

    // 删除交易一条用户信息
    public void deleteGoods(Goods goods) {
        db.delete(GOODS_TABLE_NAME, "goodsId=?", new String[]{goods.getGoodsId()});;
    }
}
